package com.fz.controller.system;

import com.fz.aop.login.AnonymousAccess;
import com.fz.common.ResponseEntity;
import com.fz.dto.system.login.LoginSysDto;
import com.fz.security.utils.SecurityUtils;
import com.fz.security.vo.LoginVo;
import com.fz.service.system.LoginService;
import com.fz.vo.system.code.CodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Api(value = "登录管理", tags = "登录管理")
@RequestMapping("/login")
@ApiSort(value = 1)
@CrossOrigin
@SuppressWarnings("unchecked")
public class LoginController{

    @Resource
    private LoginService loginService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @AnonymousAccess
    @ApiOperationSupport(order = 2)
    public ResponseEntity<LoginVo> login(HttpServletRequest request, @RequestBody LoginSysDto loginSysDto) throws Exception {
        return loginService.login(loginSysDto.getLoginid(),loginSysDto.getPassword(),
                loginSysDto.getRandCode(),loginSysDto.getRandCodeKey(),request);
    }

    @AnonymousAccess
    @GetMapping("/randCode")
    @ApiOperation(value = "获取验证码")
    @ApiOperationSupport(order = 1)
    public ResponseEntity<CodeVo> randCode() throws Exception{
        return loginService.randCode();
    }

    @PostMapping("/logout")
    @ApiOperation(value = "登出")
    @ApiOperationSupport(order = 3)
    public ResponseEntity logout(HttpServletRequest request) throws Exception {
        String token = SecurityUtils.getToken();
        return loginService.logout(token,request);
    }

}
